library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
import data
data(nyc_airbnb)
nyc_airbnb =
nyc_airbnb |>
mutate(stars = review_scores_location / 2) |>
select(
borough = neighbourhood_group,
neighbourhood, stars, price, room_type, lat, long) |>
drop_na(stars) |>
filter(
borough == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500)
use plot_ly(). can pipe, but instead of aes just put ~,
geometry involves a type of plot and mode
nyc_airbnb |>
mutate(text_label = str_c("Price: $", price, "\nRating: ", stars)) |>
plot_ly(x= ~lat, y=~long, color= ~price, text= ~text_label,
type = "scatter", mode = "markers", alpha=0.5)
can zoom in and pan and hover. or select only some of the graph, like in this boxplot
nyc_airbnb |>
mutate(neighbourhood = fct_reorder(neighbourhood, price)) |>
plot_ly(y=~price, color = ~neighbourhood, type="box")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
bar plot
nyc_airbnb |>
count(neighbourhood) |>
mutate(neighbourhood = fct_reorder(neighbourhood, n)) |>
plot_ly(x=~neighbourhood, y=~n, type = "bar", color= ~neighbourhood, colors="viridis")